package com.cskaoyan.service;

import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketGoodsExample;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.GoodListVo;
import com.cskaoyan.bean.vo.TopicReadVo;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Fyy
 * @since 2022/12/29 22:53
 */

@Service
public class TopticServiceImpl implements TopticService {
    @Autowired
    MarketTopicMapper topicMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Override
    public CommonData<MarketTopic> list(BasePageInfo basePageInfo, String title, String subtitle) {
        //分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketTopicExample example = new MarketTopicExample();
        //拼接order by内容以及参数赋值
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtil.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);
        //获取查询到的列表
        List<MarketTopic> list = topicMapper.selectByExampleWithBLOBs(example);
        //分页
        PageInfo<MarketTopic> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public void create(MarketTopic marketTopic) {
        topicMapper.insertSelective(marketTopic);
    }

    @Override
    public void update(MarketTopic marketTopic) {
        topicMapper.updateByPrimaryKeyWithBLOBs(marketTopic);
    }

    @Override
    public void delete(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        topicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public TopicReadVo read(Integer id) {
        //获取页面需要的信息
        MarketTopic marketTopic = topicMapper.selectByPrimaryKey(id);
        //获取goods表
        Integer[] goods = marketTopic.getGoods();
        List<MarketGoods> list = new ArrayList<>();
        List<GoodListVo> realList = new ArrayList<>();
        //如果goods表不为空且不为空表进行查询
        if (goods != null && goods.length != 0) {
            MarketGoodsExample example = new MarketGoodsExample();
            MarketGoodsExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(Arrays.asList(goods));
            criteria.andDeletedEqualTo(false);
            list = goodsMapper.selectByExampleWithBLOBs(example);
            realList = new ArrayList<>();
            for (MarketGoods marketGoods : list) {
                GoodListVo goodListVo = new GoodListVo();
                BeanUtils.copyProperties(marketGoods,goodListVo);
                realList.add(goodListVo);
            }
        }
        TopicReadVo topicReadVo = new TopicReadVo();
        //给返回值进行赋值
        topicReadVo.setGoodsList(realList);
        topicReadVo.setTopic(marketTopic);
        return topicReadVo;
    }
}
